\ProvidesPackage{pytex}
\RequirePackage{verbatim}
\RequirePackage{fancyvrb}
\RequirePackage{listings}
\RequirePackage{color}
\RequirePackage{etoolbox}

\newcounter{pytexcount}
\newcounter{pytexsubcount}
\newcounter{pytexlinecountstart}
\newcounter{pytexlinecountend}

\setcounter{pytexcount}{0}
\setcounter{pytexlinecountstart}{0}
\setcounter{pytexlinecountend}{0}


\newwrite\PYTEX@sf
\newwrite\PYTEX@tempfile
\newtoks\PYTEX@acc

\definecolor{bg}{RGB}{220,220,220}

\lstdefinestyle{python}{columns=flexible,language=Python,showstringspaces=false,basicstyle=\ttfamily,commentstyle=\ttfamily}
\lstdefinestyle{text}{columns=flexible,basicstyle=\ttfamily}

\def\@enamedef#1{\expandafter\def\csname #1\expandafter\endcsname\expandafter}

\@namedef{pytex-empty}{}
% Assign a new header
\newenvironment{pytexTemplate}[1]
{%
  \global\PYTEX@acc={}%   clear accumulator
  \def\verbatim@processline{\global\PYTEX@acc=\expandafter{\the\expandafter\PYTEX@acc\the\verbatim@line^^J}}%   each line is added to accumulator
  \def\verbatim@finish{\global\@enamedef{pytex-#1}{\the\PYTEX@acc}}% Store accumulator content to pytex-#1
  \verbatim%
}
{
\endverbatim%
}

% The default header is empty
\@namedef{pytex-empty}{}

\newcommand{\pytexStart}[1]{
  \addtocounter{pytexcount}{1}%   pytexcoun++
  \setcounter{pytexsubcount}{0}%  reset pytexsubcount
  \immediate\openout\PYTEX@sf=pytex_\alph{pytexcount}.py\relax%  open file for writing
  \immediate\write\PYTEX@sf{##=== pytex::#1 ====}%  write information to file
  \immediate\write\PYTEX@sf{\@nameuse{pytex-#1}}%   write header to file
}



\newenvironment{pytex}
{%
  \addtocounter{pytexsubcount}{1}%                                             pytexsubcount++
  \immediate\write\PYTEX@sf{print("pytex snippet::\arabic{pytexsubcount}")}%    print separator to stdout
  \global\PYTEX@acc={}%                                                        clear accumulator
  %                                                                            each line is added to accumulator
  \let\verbatim@processlinebackup\verbatim@processline%
  \immediate\openout\PYTEX@tempfile=temp_pytex.py\relax%  open file for writing
  \def\verbatim@processline{\immediate\write\PYTEX@sf{\the\verbatim@line}\immediate\write\PYTEX@tempfile{\the\verbatim@line}}%
  \let\verbatimbackup\@verbatim
  \def\@verbatim{ \topsep=-17pt \partopsep=0pt \verbatimbackup}
 \verbatim%
}%
{\endverbatim%
 \let\verbatim@processline\verbatim@processlinebackup%
 \let\@verbatim\verbatimbackup%
 \immediate\closeout\PYTEX@tempfile%
  \IfFileExists{pytex_\alph{pytexcount}_\arabic{pytexsubcount}.log}{%
    \lstset{style=python} %
    \lstinputlisting[]{temp_pytex.py}%
    \lstset{style=text,aboveskip=0pt} %
    \lstinputlisting[frame=leftline,xleftmargin=5mm]{pytex_\alph{pytexcount}_\arabic{pytexsubcount}.log}%
 }{%
    \lstset{style=python}%
    \lstinputlisting[]{temp_pytex.py}%
 }% 
}

\newenvironment{pytexoutput}
{%
  \addtocounter{pytexsubcount}{1}%                                             pytexsubcount++
  \immediate\write\PYTEX@sf{print("pytex snippet::\arabic{pytexsubcount}")}%    print separator to stdout
  \global\PYTEX@acc={}%                                                        clear accumulator
  %                                                                            each line is added to accumulator
  \let\verbatim@processlinebackup\verbatim@processline%
  \immediate\openout\PYTEX@tempfile=temp_pytex.py\relax%  open file for writing
  \def\verbatim@processline{\immediate\write\PYTEX@sf{\the\verbatim@line}\immediate\write\PYTEX@tempfile{\the\verbatim@line}}%
  \let\verbatimbackup\@verbatim
  \def\@verbatim{ \topsep=-17pt \partopsep=0pt \verbatimbackup}
 \verbatim%
}%
{\endverbatim%
 \let\verbatim@processline\verbatim@processlinebackup%
 \let\@verbatim\verbatimbackup%
 \immediate\closeout\PYTEX@tempfile%
  \IfFileExists{pytex_\alph{pytexcount}_\arabic{pytexsubcount}.log}{%
    \lstset{style=text,aboveskip=1ex} %
    \lstinputlisting[frame=leftline,xleftmargin=5mm]{pytex_\alph{pytexcount}_\arabic{pytexsubcount}.log}%
 }{%
 }% 
}
